--
-- Author: mm
-- Date: 2019-03-09 11:16:54
--
-- NewpartnerController
--

local PartnerView = import("..views.PartnerView")
local EventManager = require("app.public.eventmanager.EventManager")
local PartnerNewView = import("..views.PartnerNewView")
require("app.ui.DelayCacheMgr")

local fileName = string.getModeName(...)
local PartnerController = class(fileName, app.mvc.ControllerBase)

function PartnerController:ctor(data)
    self.super.ctor(self, data)
end

function PartnerController:init()
    self._model = app:getInst("PartnerModel")
    self._partnerPlayChangge = UD.player:onChanged(handler(self, self._onPartnerPlayerChange))
    self._partnerChange = UD:onChanged("partner", handler(self, self._onPartnerDataChange))
end

function PartnerController:removeListeners()
    self.super.removeListeners(self)
    UD.baplayerg:unChanged(self._partnerPlayChangge)

    UD:unChanged(self._partnerChange)
end

function PartnerController:onEvent(eventName, data, opt)
    if eventName == ViewEvent.Partner_DataChange then
        if opt == "insert" then
            DelayCacheMgr.registerDelayMsg(DelayCacheMgr.DelayConstant.GetPartner,handler(self, self.openNewView), data)
        end
        if data then
            local equips = data:getEquips()
            for k , v in pairs(equips) do
                if v:isNewUnlock() then
                    UIHandler:showThirdLevelTips( { remindId = Constant.FUNC_TYPE_MERCENARY, params = {partnerName = data:getName(), equipName = v:getName(), iconPath = v:getIcon()} } )
                    v:setNewUnlock(nil)
                end
            end
        end
    elseif eventName == "onMapChanged" then
		if data then
            UD.partnersData:C2S_QuerySuperTitlePartner()
		end
    end
end

function PartnerController:listEvent()
    return {ViewEvent.Partner_DataChange}
end

function PartnerController:_onPartnerPlayerChange(event, oldPartners, newPartners)
end

function PartnerController:_onPartnerDataChange()
end

function PartnerController:openView(data, isStoryUI)
    if nil == self._view then
        if isStoryUI then
            self:setParentType("storyUI")
        end
        local PartnerView = require("app.partner.views.PartnerView")
        
        self._view = PartnerView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        -- 由于服务器之前金腰带逻辑 只传了 id 战力 等信息没传
        -- 这里打开界面的时候 拉取一下金腰带随从的 战力 等信息
        UD.partnersData:C2S_QuerySuperTitlePartner()
    end
    self:addChild(self._view, data)
    self._view:show()
end

function PartnerController:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end

function PartnerController:openViewByStory(data)
    self:openView(data, true)
end


function PartnerController:openPartnerEquipStrengthView(data)
    local PartnerEquipStrenghPop = require("app.partner.views.PartnerEquipStrenghPop")
    local view = PartnerEquipStrenghPop.new(nil, data)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    view:onOperate(handler(self, self.onOperate))
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end


function PartnerController:openPartnerSkillLevelUpPop(data)
    local PartnerEquipStrenghPop = require("app.partner.views.PartnerSkillLevelUpPop")
    local view = PartnerEquipStrenghPop.new(nil, data)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    view:onOperate(handler(self, self.onOperate))
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end


--获取新随从
function PartnerController:openNewView(partner)
    if BattleManager:isBattle() or StoryManager:isEnterStoryMode() then
        self.partnerCache = partner
        DelayStack:pushToFront({name = "newPartner"})
    else
        UIHandler:getPopupUi():removeUIByKey(PartnerNewView:getViewName())

        local view = PartnerNewView.new()
        view:onCloseView(function(sender)
            UIHandler:removePopupUiByKey(sender:getViewName())
        end)
        view:onOperate(function(op, params)
            if op == "sharePartner" then
                display.pushToast(L("lua_code_text_493"))
            end
        end)
        view:refresh(partner)
        UIHandler:addPopupUi(view, view:getViewName())
        if partner then
            view:show()
        end
    end
end

function PartnerController:openPartnerGetPop(data)
    local PartnerGetPop = require("app.partner.views.PartnerGetPop")
    local view = PartnerGetPop.new(nil, data)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    view:onOperate(handler(self, self.onOperate))
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end


function PartnerController:openPartnerLvUpQualityView(data)
    local PartnerQualityLvUpView = require("app.partner.views.PartnerQualityLvUpView")
    local view = PartnerQualityLvUpView.new(nil, data)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    view:onOperate(handler(self, self.onOperate))
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end


-------------------------------------------------------

-------------------------------------------------------

function PartnerController:onOperate(op, data)
    print("NewpartnerController:onOperate op = " .. op)
    if op == "openPartnerEquipView" then
        self:openPartnerEquipStrengthView(data)
    elseif op == "openPartnerSkillUpPop" then
        self:openPartnerSkillLevelUpPop(data)
    elseif op == "openPartnerGetPop" then
        self:openPartnerGetPop(data)
    elseif op == "openPartnerQualityLvUp" then
        self:openPartnerLvUpQualityView(data)
    elseif op == "startLvUpPartnerQuality" then
        --品质提升
        self._model:reqLvUpQuality(data)
    elseif op == "startUnlockPartner" then
        --解锁partner
        self._model:reqUnlockPartner(data:getId())
    end
end


return PartnerController
